package com.gift.controller;

import com.gift.common.PaGing;
import com.gift.common.ServerResponse;
import com.gift.common.TimeHandler;
import com.gift.pojo.SeTable;
import com.gift.pojo.UserTable;
import com.gift.service.SpecialDealService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("specialDeal")
public class SpecialDealController {

    @Resource
    SpecialDealService specialDealService;

    //跳转到特殊交易信息首页
    @RequestMapping("/home")
    public String goHome(){
        return "SpecialDealController/specialDealQuery";
    }


    //得到所有所有特殊交易信息
    @RequestMapping("/getAll")
    @ResponseBody
    public PaGing getAll(int page, int rows,Map<String,String> map){
        Map<String,Object> mapO ;
        mapO = TimeHandler.findParmHandler(map);
        return specialDealService.getAllBypr(page,rows,mapO);
    }

    //跳转到新增特殊交易页面
    @RequestMapping("/toInsetSpecialDeal")
    public String toInsetSpecialDeal(Model model){
        Subject currentUser = SecurityUtils.getSubject();
        UserTable user=((UserTable) currentUser.getPrincipal());
        model.addAttribute("userID",user.getUid());
        model.addAttribute("userRName",user.getRelname());
        return "SpecialDealController/newSpecialDeal";
    }

    //保存新增特殊交易信息
    @RequestMapping(value = "/insetSpecialDeal" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse insetSpecialDeal(SeTable newSeTable){

        return specialDealService.insetnewSeTable(newSeTable);
    }


    //跳转到修改特殊交易页面
    @RequestMapping("/toupdataSpecialDeal")
    public String toupdataSpecialDeal(int sid, Model model){

        SeTable seTable=specialDealService.getSeTableBySid(sid);
        Subject currentUser = SecurityUtils.getSubject();
        UserTable user=((UserTable) currentUser.getPrincipal());
        model.addAttribute("userID",user.getUid());
        model.addAttribute("userRName",user.getRelname());
        model.addAttribute("seTable",seTable);
        return "SpecialDealController/updataSpecialDeal";
    }

    //修改特殊交易信息
    @RequestMapping(value = "/updataSpecialDeal",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse updataSpecialDeal(SeTable updataSeTable){
        return specialDealService.updataSeTable(updataSeTable);
    }

    //删除特殊交易信息
    @RequestMapping(value = "deleteSpecialDeal", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse deleteSpecialDeal(@Param("deleteIdString") int deleteId ) {

        return  specialDealService.deleteSid(deleteId);
    }



}
